package com.mdc.mansion.common.util;

/**
 * MMBOSS
 * @(#) BeanUtils.java 2010-9-16 上午10:07:17
 * Copyright 2010 12530.com, Inc. All rights reserved.
 */

import com.mdc.mansion.common.util.builder.CustomToStringBuilder;
import org.apache.commons.lang.ClassUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;

/**
 * <p>
 * Title:JavaBean工具类
 * </p>
 * <p>
 * Description:JavaBean工具类
 * </p>
 * 
 * @company 12530.com
 * @author xuyang
 * @version 1.0
 */
public class BeanUtils extends org.springframework.beans.BeanUtils{

	private BeanUtils() {
	}

	/** 分隔符 */
	public static final String SEPARATOR = "\t";

	/**
	 * 获取JavaBean信息
	 * 
	 * @param obj
	 * @return “属性”=“值”
	 */
	public static String getBeanInfo(Object obj) {
		CustomToStringBuilder builder = new CustomToStringBuilder(obj);
		return builder.toString();
	}

	/**
	 * 判断obj是否是集合
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isCollection(Object obj) {
		return obj instanceof Collection<?> ? true : false;
	}

	/**
	 * 判断obj是否是基本数据类型或String类型
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isPrimitive(Object obj) {
		return (obj instanceof Number || obj instanceof Boolean || obj instanceof Character || obj instanceof CharSequence);
	}

	/**
	 * 判断obj是否是数组
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isArray(Object obj) {
		return obj.getClass().isArray();
	}

	/**
	 * 判断obj是否是Enumeration
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEnumeration(Object obj) {
		return obj instanceof Enumeration<?> ? true : false;
	}

	/**
	 * 判断obj是否是枚举
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEnum(Object obj) {
		return obj instanceof Enum<?> ? true : false;
	}

	/**
	 * 判断obj是否是Map
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isMap(Object obj) {
		return obj instanceof Map<?, ?> ? true : false;
	}

	/**
	 * 判断field是否是内部类
	 * 
	 * @param field
	 * @return
	 */
	public static boolean isInnerClass(Field field) {
		return (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1);
	}

	/**
	 * 判断field是否是静态的
	 * 
	 * @param field
	 * @return
	 */
	public static boolean isStatic(Field field) {
		int mod = field.getModifiers();
		return Modifier.isStatic(mod);
	}

	/**
	 * 判断field是否是transient
	 * 
	 * @param field
	 * @return
	 */
	public static boolean isTransient(Field field) {
		return Modifier.isTransient(field.getModifiers());
	}

	/**
	 * 替换回车换行符为BeanUtils.SEPARATOR
	 * 
	 * @param src
	 * @return
	 */
	public static String replace(String src) {
		if (src != null)
			src = src.replaceAll("[\r\n]+", SEPARATOR);
		return src;
	}
}
